package com.camerareader.mvp.splash_page;

import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.camerareader.R;
import com.camerareader.mvp.login_page.LoginActivity;
import com.camerareader.mvp.main_page.MainActivity;
import com.camerareader.mvp.main_page.MainActivityContract;
import com.fileoperation.CommonUtils;
import com.fileoperation.model.LogOutEvent;
import com.fileoperation.user.UserInfo;
import com.jakewharton.rxbinding.view.RxView;
import com.nettools.NetUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class SplashActivity extends AppCompatActivity {

    private View logIn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        UserInfo.getInstance().initUserInfo(this);
//        initView();
        getWindow().getDecorView().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (UserInfo.getInstance().isLogin()){
                    Observable.create(new Observable.OnSubscribe<Boolean>() {
                        @Override
                        public void call(Subscriber<? super Boolean> subscriber) {
//                            try {
//                                NetUtils.getInstance().refreshAccessToken();
//                            } catch (IOException e) {
//                                e.printStackTrace();
//                            } catch (JSONException e) {
//                                e.printStackTrace();
//                            }
                            subscriber.onNext(true);
                        }
                    })
                            .subscribeOn(Schedulers.newThread())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new Subscriber<Boolean>() {
                                @Override
                                public void onCompleted() {

                                }

                                @Override
                                public void onError(Throwable e) {
                                    EventBus.getDefault().post(new LogOutEvent());
                                    Toast.makeText(SplashActivity.this, "请登录！", Toast.LENGTH_SHORT).show();
                                }

                                @Override
                                public void onNext(Boolean aBoolean) {
                                }
                            });
                    MainActivity.navigateMainActivity(SplashActivity.this);
                    finish();
                }else {
                    LoginActivity.navigateLoginActivity(SplashActivity.this);
                    finish();
                }
            }
        }, 100);
    }

//    private void initView() {
//        logIn = findViewById(R.id.log_in);
//        RxView.clicks(logIn).throttleFirst(1, TimeUnit.SECONDS)
//                .subscribe(new Action1<Void>() {
//                    @Override
//                    public void call(Void aVoid) {
//                        LoginActivity.navigateLoginActivity(SplashActivity.this);
//                        finish();
//                    }
//                });
//
//        TextView arrow = (TextView) findViewById(R.id.arrow);
//        CommonUtils.resizeDrawable(getResources().getDimensionPixelSize(R.dimen.right_arrow_size), 2, arrow);
//    }
}
